<template>
  <div class="login">
    <div class="login_way">
      <el-form
        v-if="true"
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <!-- <div class="top_pic" @click="changeWay">
          <img v-if="way === false" src="/assets/images/login/code.png" />
          <img v-if="way === true" src="/assets/images/login/computer.png" />
        </div> -->

        <div class="code_login" v-if="way === true">
          <div class="code_title">欢迎登录宁环通</div>
          <div class="code_subtitle">请使用微信扫一扫登录</div>
          <div class="code_pic">
            <div style="display: inline-block;width: 160px;height: 160px;" ref="wxCode"></div>
            <div 
            @click="refreshCode"
            v-show="maskCode"
            style="
            top:0px;left: 50%;
            transform: translate(-80px, 0px);
            position: absolute;
            line-height: 160px;
            text-align: center;
            color: white;
            font-size: 50px;
            background: rgba(0, 0, 0, 0.7);width: 160px;height: 160px">
            <i class="el-icon-refresh-right"></i>
            </div>

            <!-- <el-image v-if="row.avatar" :src="'data:image/jpeg;base64,' + row.avatar"
                  :preview-src-list="['data:image/jpeg;base64,' + row.avatar]">
            </el-image> -->
          </div>
          <span> 授权即同意 <a>服务协议</a> 和 <a>隐私政策</a> </span>
        </div>

        <div v-if="way === false">
          <div class="header">
            <div class="title">欢迎使用</div>
            <span class="subtitle">用户名密码</span>
          </div>

          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
              <svg-icon
                slot="prefix"
                icon-class="user"
                class="el-input__icon input-icon"
              />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon
                slot="prefix"
                icon-class="password"
                class="el-input__icon input-icon"
              />
            </el-input>
          </el-form-item>
          <!-- <el-form-item prop="code" v-if="captchaEnabled">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon
                slot="prefix"
                icon-class="validCode"
                class="el-input__icon input-icon"
              />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img" />
            </div>
          </el-form-item> -->
          <el-checkbox
            v-model="loginForm.rememberMe"
            style="margin-bottom: 20px"
            >记住密码</el-checkbox
          >
          <el-form-item style="width: 100%">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width: 100%"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right" v-if="register">
              <router-link class="link-type" :to="'/register'"
                >立即注册</router-link
              >
            </div>
          </el-form-item>
          <!-- <div class="check">
            <el-checkbox v-model="agreement">
              我已阅读并同意 <a>服务协议</a> 和 <a>隐私政策</a>
            </el-checkbox>
          </div> -->
        </div>
      </el-form>

      <div class="user_list" v-if="false">
        <div class="user_title">选择要登录的账号</div>
        <div class="user_content">
          <el-row class="row" :gutter="10" v-for="item in [1, 2, 3]">
            <el-col :span="4">
              <div class="user_avatar"></div>
            </el-col>
            <el-col :span="4">
              <div class="user_name">张三</div>
            </el-col>
            <el-col :span="16">
              <div class="user_agency">
                @宁夏生态环境厅/银川市生态环境局/兴庆分局/大气监测科
              </div>
            </el-col>
          </el-row>
        </div>
      </div>

      <!-- <div class="free">要免费试用？<a>请联系我</a></div> -->
    </div>

    <div class="login_picture">
      <!-- <div class="text">
        <span>种一棵树最好的时机是十年以前 其次就是现在</span>
      </div> -->
      <div class="picture">
        <img src="@/assets/images/square.png" />
      </div>
    </div>
  </div>

</template>

<script>
import { getCodeImg } from "@/api/login";
import { decrypt, encrypt } from "@/utils/jsencrypt";
import Cookies from "js-cookie";

export default {
  name: "Login",
  data() {
    return {
      way: false,
      agreement: false,
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
        console.log("参数", route);
      },
      immediate: true,
    },
  },
  created() {
    // this.dengl();
    // this.getCookie();
  },
  methods: {
    dengl() {
      const params = new URLSearchParams({
        grant_type: "authorization_code",
        response_type: "code",
        client_id: "4de8e00428f1441886f899812cda50ac",
        state: "state",
        redirect_uri: "https://farm.nxnycy.cn",
      });
      // 跳转到目标 URL
      window.location.href =
        "https://auth2.nxeduyun.com/oauth2/v1/connect/authorize?" +
        params.toString();
    },
    //获取验证码
    getCode() {
      getCodeImg().then((res) => {
        this.captchaEnabled =
          res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then((res) => {
              // this.$router.push({path:"/HelloWorld"})
              // return
              this.$router.push({ path: this.redirect || "/" }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  // font-family: 思源黑体;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.9);

  .free {
    margin-top: 13px;
    font-size: 14px;
  }
  a {
    color: rgb(22, 93, 255);
  }
  .login_way {
    .user_list {
      box-shadow: 2px 4px 11px 0px rgba(140, 139, 139, 0.2);
      backdrop-filter: blur(8px);
      background: rgba(255, 255, 255, 0.7);
      width: 400px;
      height: 480px;
      overflow: auto;
      border-radius: 8px;
      padding: 64px 44px;
      .user_content {
        .row {
          padding: 16px 0px;
          border-bottom: 1px dashed rgb(229, 230, 235);
        }
        .user_agency {
          color: rgb(237, 123, 47);
          font-size: 12px;
        }
        .user_name {
          color: rgb(29, 33, 41);
          font-size: 16px;
        }
        .user_avatar {
          width: 32px;
          height: 32px;
        }
      }
      .user_title {
        text-align: center;
        color: rgb(29, 33, 41);
        font-size: 24px;
        font-weight: 500;
        margin-bottom: 48px;
      }
    }

    width: 50%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .login-form {
      height: 530px;
      .code_login {
        text-align: center;
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        .code_title {
          color: rgba(0, 0, 0, 0.9);
          font-size: 36px;
          font-weight: 500;
        }
        .code_subtitle {
          margin: 34px 0px 20px;
        }
        .code_pic {
          position: relative;
          img {
            width: 160px;
            height: 160px;
          }
          margin-bottom: 40px;
        }
      }
      .top_pic {
        position: absolute;
        top: 0;
        right: 0;
        cursor: pointer;
      }
      position: relative;
      box-shadow: 2px 4px 11px 0px rgba(140, 139, 139, 0.2);
      backdrop-filter: blur(8px);
      background: rgba(255, 255, 255, 0.7);
      width: 400px;
      border-radius: 8px;
      padding: 64px 44px;
      .header {
        margin-bottom: 32px;

        .title {
          color: rgba(0, 0, 0, 0.9);
          font-size: 36px;
          font-weight: 500;
          margin-bottom: 14px;
        }
        .subtitle {
          color: rgb(29, 33, 41);
          font-size: 24px;
          font-weight: 500;
        }
      }
      .el-input {
        height: 32px;
        input {
          height: 32px;
        }
      }
      .el-input__inner {
        border: 1px solid rgb(201, 205, 212);
      }
      .el-form-item {
        margin-bottom: 16px;
      }
      .input-icon {
        height: 39px;
        margin-left: 2px;
      }
    }
  }
  .login_picture {
    flex: 1;
    position: relative;
    justify-content: center;
    display: flex;

    .text {
      position: absolute;
      left: 50%;
      transform: translate(-50%);
      text-wrap: nowrap;

      width: fit-content;
      text-align: center;
      padding: 30px 20px;
      box-sizing: border-box;
      border-left: 1.5px solid rgba(255, 255, 255, 0.6);
      border-top: 1.5px solid rgba(255, 255, 255, 0.6);
      border-radius: 6px;
      /* 透明卡片 */
      box-shadow: 2px 4px 11px 0px rgba(140, 139, 139, 0.2);
      backdrop-filter: blur(8px);
      background: rgba(183, 183, 183, 0.13);
      span {
        color: rgb(29, 33, 41);
        // font-family: 思源黑体;
        font-size: 24px;
        font-weight: 500;
      }
    }
    img {
      width: 744px;
      height: 670px;
    }
  }
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/loginbg.png");
  background-size: cover;
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}

</style>
